package org.example.aienglishapp_login.util.response;

import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;

/**
 * @author Asus
 */
@Setter
@Getter
public class ResponseResult<T> implements Serializable {

    /**
     * 成功与否
     */
    private Boolean success;

    /**
     * 状态码
     */
    private int code;

    /**
     * 消息
     */
    private String message;

    /**
     * 数据
     */
    private T data;

    // 默认构造函数
    public ResponseResult() {
    }

    // 带参数的构造函数
    public ResponseResult(Boolean success, int code, String message, T data) {
        this.success = success;
        this.code = code;
        this.message = message;
        this.data = data;
    }

    // 静态方法 - 成功响应（带有数据）
    public static <T> ResponseResult<T> success(T data) {
        ResponseResult<T> responseResult = new ResponseResult<>();
        responseResult.setSuccess(ResultCode.SUCCESS.getSuccess());
        responseResult.setCode(ResultCode.SUCCESS.getCode());
        responseResult.setMessage(ResultCode.SUCCESS.getMessage());
        responseResult.setData(data);
        return responseResult;
    }

    // 静态方法 - 成功响应（不带数据）
    public static <T> ResponseResult<T> success() {
        ResponseResult<T> responseResult = new ResponseResult<>();
        responseResult.setSuccess(ResultCode.SUCCESS.getSuccess());
        responseResult.setCode(ResultCode.SUCCESS.getCode());
        responseResult.setMessage(ResultCode.SUCCESS.getMessage());
        return responseResult;
    }

    // 静态方法 - 失败响应
    public static <T> ResponseResult<T> failed(int code, String message) {
        ResponseResult<T> responseResult = new ResponseResult<>();
        responseResult.setSuccess(ResultCode.NOT_FOUND.getSuccess());
        responseResult.setCode(code);
        responseResult.setMessage(message);
        return responseResult;
    }

    // 静态方法 - 检查响应是否成功
    public static boolean isSucceed(ResponseResult<?> responseResult) {
        return responseResult.getCode() == ResultCode.SUCCESS.getCode();
    }
}